// pages/index/classify/classify/videoDetail/videoDetail.js
//试看弹框路径
import Toast from '../../../../miniprogram_npm/vant-weapp/toast/toast'
const appInstance = getApp();
var url = appInstance.globalData.url;
var Id = '';
var Height = '';
var videoDuration = '';
var videoCurrentTime = '';
Page({
  /**
   * 页面的初始数据
   */
  data: {
    zan: true,
    show: false,
    isShow: true,
    videoDetailUrl: '',
    videoName: '',
    videoSubheadings: '',
    Recommended: [],
    videoCover: '',
    Try_duration: '',
    Height: '800px',
    Category_name: '',
    loadding: false,
    pageCurrent: 1,
    Is_charge: '',
    hide: true,
    picShow:true,
    video:false
  },
  onClose() {
    // console.log(1)
    this.setData({
      show: false
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
  
    if (!wx.getStorageSync('is_member')) {
      var is_member = 0;
    } else {
      var is_member = wx.getStorageSync('is_member')
    }
    console.log(options.id)
    Id = options.id
    this.getVideoDetail()
    this.setData({
      is_member: wx.getStorageSync('is_member')
      // is_member:1
    })

  },

  // 点击封面的处理函数
  handleClickCover () {
    this.hideCover();
    this.playVideo();
  },
  // 隐藏封面
  hideCover (){
    this.setData({
      picShow:false,
    })
  },

  // 播放视频
  playVideo () {
    this.videoContext.play()
  },

  videoInit() {
    // 是会员 或者 视频免费的时候,需要手动处理
    if (this.data.is_member == 2 || this.data.Is_charge == 0) {
      this.hideCover();
      return;
    }

    // 不是会员,而且视频收费的时候: 试看!(默认)
    Toast({
      duration: '5000',
      message: '您可以免费试看' + this.data.Try_duration + '秒',
      position: 'top',
      zIndex: '10000'
    })
  },


  //视频时长
  getVideoDuration() {
    var that = this;
    wx.request({
      url: url + '/user/saveHistory',
      method: 'POST',
      data: {
        video_id: Id,
        pp_token: wx.getStorageSync('to_ken'),
        duration: videoCurrentTime
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      dataType: 'json',
      success(res) {
        // console.log(res.data.code)
        if (res.data.code == 200) {
          // wx.navigateBack({
          //   delta: 1
          // })
        }
      }
    })
  },
  //播放进度变化时触发
  shichngTap(e) {
    // console.log(e.detail.duration)
    //时长
    videoCurrentTime = e.detail.currentTime
    console.log(e.detail.currentTime)
    videoDuration = e.detail.duration
    // console.log(videoDuration)
  },
  //当开始/继续播放时触发play事件
  // startTap(e) {
  //   console.log(e)
  // },


  //详情页
  getVideoDetail() {
    var that = this;
    wx.request({
      url: url + '/video/detail',
      method: 'POST',
      data: {
        video_id: Id,
        pp_token: wx.getStorageSync('to_ken')
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      dataType: 'json',
      success(res) {
        var try_duration = res.data.data.try_duration;
        var Url = res.data.data.url;
        var Name = res.data.data.name;
        var Subheadings = res.data.data.description;
        var Hits = res.data.data.count_visit;
        var Recommended = res.data.data.hot_videos;
        var category_name = res.data.data.category_name;
        var videoCover = res.data.data.cover;
        that.setData({
          videoDetailUrl: Url,
          videoName: Name,
          videoSubheadings: Subheadings,
          videoHits: Hits,
          Recommended: Recommended,
          videoCover: videoCover,
          Try_duration: try_duration,
          Category_name: category_name,
          Is_charge: res.data.data.is_charge
        })
        that.videoInit();
      }
    })
  },

  //试看视频结束事件
  tiaoZmember() {
    console.log(1)
    wx.switchTab({
      url: '../../../member/member' //注意switchTab只能跳转到带有tab的页面，不能跳转到不带tab的页面
    })
  },


  //点赞
  zanTap() {
    this.setData({
      zan: !this.data.zan
    })
  },
  //收藏
  shouCang() {
    var that = this;
    // console.log(wx.getStorageSync('to_ken'))
    var token = wx.getStorageSync('to_ken')
    wx.request({
      url: url + '/follow/index',
      method: "POST",
      data: {
        pp_token: token,
        video_id: Id,
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      dataType: 'json',
      success(res) {
        console.log(res.data.code)
        if (res.data.code == 200) {
          wx.showToast({
            title: "收藏成功",
            icon: 'success',
            duration: 2000
          })

          that.setData({
            isShow: false
          })
        } else if (res.data.code == 501) {
          wx.showToast({
            title: "您已收藏",
            icon: 'success',
            duration: 2000
          })

          that.setData({
            isShow: true
          })

        }
      }
    })

  },

  //转发
  // onShareAppMessage: function(res) {
  //   if (res.from === 'button') {}
  //   return {
  //     title: '篮梦视频',
  //     path: '/pages/index/classify/videoDetail/videoDetail/videoDetail',
  //     success: function(res) {
  //       console.log('成功', res)
  //     }
  //   }
  // },


  // 跳转到视频简介
  introducesTap() {
    var that = this;
    //弹框高度
    var query = wx.createSelectorQuery();
    query.select('#getheight').boundingClientRect()
    query.exec(function(res) {
      console.log(res);
      Height = res[0].height
      console.log(Height)
      console.log(res[0].height);
    })
    wx.getSystemInfo({
      success: function(res) {
        Height = res.screenHeight - 225
        console.log(res.screenHeight - 225)
        that.setData({
          Height: res.screenHeight - 335
        })
      },
    })
    this.setData({
      show: true
    })
  },

  //跳转到详情
  videoDetail(e) {
    console.log(e.currentTarget.id)
    var Id = e.currentTarget.id;
    wx.navigateTo({
      url: '../videoDetail/videoDetail?id=' + Id,
    })

  },

  preventD() {
    return
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    this.videoContext = wx.createVideoContext('myVideo')

  },

  // bindPlay: function () {
  //   this.videoContext.play()
  // },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    this.getVideoDuration();
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    var that = this;
    that.setData({
      loadding: true
    })
    wx.request({
      url: url + '/video/list',
      method: 'POST',
      data: {
        page: that.data.pageCurrent,
        size: 4
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      dataType: 'json',
      success(res) {
        console.log(res.data.code)
        that.setData({
          loadding: false
        })
        if (res.data.data.length !== 0) {
          that.setData({
            Recommended: that.data.Recommended.concat(res.data.data),
            pageCurrent: that.data.pageCurrent + 1
          })
        }
      }
    })


  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})